function dt=var_select_neighbor_w_val(dt)

%VAR_SELECT_NEIGHBOR_W_VAL double validation on variable selection with weighting
%Syntax: dt=var_select_neighbor_w_val(dt)
%Description: leave-one-out validation of leave-one-out crossvalidated
%             variable selection, with weighting
% 
%Jonas Almeida 18 Feb 2006

x=dt.nx;y=dt.y;
[n,m]=size(dt.x);
k=length(dt.y(1,:));
dt.predict.var=[];
dt.predict.dy=[];
dt.predict.w=[];
for i=1:n
    xi=x;yi=y;
    xi(i,:)=[];yi(i,:)=[];
    [var,w,dy]=var_select_neighbor_w(xi,yi);
    dt.predict.var=[dt.predict.var;var];
    dt.predict.dy=[dt.predict.dy;dy];
    dt.predict.w=[dt.predict.w;w];
    disp([num2str(i),': var selected / cv error'])
    disp(var(1:10));disp(dy(1:10));
    for j=1:m %for each model make a prediction
        dt.predict.y(i,j)=neighbors_y(repmat(w(1:j),n-1,1).*xi(:,var(1:j)),yi,w(1:j).*x(i,var(1:j)));
    end
    %figure;s=find(yi==1);n=find(yi==0);plot3(xi(n,var(1)),xi(n,var(2)),xi(n,var(3)),'o','MarkerFaceColor','g');hold on;plot3(xi(s,var(1)),xi(s,var(2)),xi(s,var(3)),'o','MarkerFaceColor','r');grid on;xlabel('Var 1');plot3(x(i,var(1)),x(i,var(2)),x(i,var(3)),'*');
    %title([num2str(i),': should be ',num2str(y(i)),' predicted: ',num2str(dt.predict.y(i,1:3))])
    dt.predict.err(i,:)=~(dt.predict.y(i,:)==dt.y(i));
end
